package com.yj.lcj.utils;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

/**
 * jwt生成token配置类
 */
public class JwtUtil {


    // 签名
    private static final String SIGN = "JYLCJ";

    public static String getToken(Map<String, String> map){
        // 创建JWT对象
        JWTCreator.Builder jwtCreator = JWT.create();

        // 封装用户信息到jwt
        map.forEach((k,v)->{
            jwtCreator.withClaim(k, v);
        });

        // 设置过期时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 4);;

        jwtCreator.withExpiresAt(calendar.getTime());

        // 签名  生成token
        String token = jwtCreator.sign(Algorithm.HMAC256(SIGN));

        return token;
    }

    /**
     * 验证token
     * @param token
     * @return
     */
    public static DecodedJWT verifyToken(String token){
        DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
        return decodedJWT;

    }


    /**
     * 退出
     */
    public static void deleteToken(String token){

    }
}
